/**
 * @file appShell/common module
 * @author flower.cheng(flower.cheng@boqii.com)
 */
export const SET_PAGE_SWITCHING = 'SET_PAGE_SWITCHING';
export const SET_PAGE_SCROLL_POSITION = 'SET_PAGE_SCROLL_POSITION';
export const SAVE_PAGE_COMMON_PARAMS = 'SAVE_PAGE_COMMON_PARAMS';
export const SET_VIDEO_OBJ = 'SET_VIDEO_OBJ';

export const state = () => {
  return {
    /**
     * 多个页面是否处于切换中
     *
     * @type {boolean}
     */
    isPageSwitching: false,

    /**
     * 保存页面滚动位置，以 `route.fullPath` 为键
     * {'/': 0, '/detail/1': 100, '/detail/2': 200}
     *
     * @type {Object}
     */
    scrollPostionMap: {},

    /**
     * 保存浏览器query上的配置参数参数
     *
     * @type {Object}
     */
    commonBiz: {},
    globalVideoObj: {}
  };
};

export const mutations = {
  [SET_PAGE_SWITCHING](state, isPageSwitching) {
    state.isPageSwitching = isPageSwitching;
  },

  [SET_PAGE_SCROLL_POSITION](state, {
    pageId,
    scrollPosition
  }) {
    state.scrollPostionMap = {
      ...state.scrollPostionMap,
      [pageId]: scrollPosition
    };
  },

  [SAVE_PAGE_COMMON_PARAMS](state, data) {
    state.commonBiz = data;
  },
  [SET_VIDEO_OBJ](state, data) {
    state.globalVideoObj = data;
    console.log("state.globalVideoObj", state.globalVideoObj)
  },
};

export const actions = {

  /**
   * 设置页面是否处于切换中
   *
   * @param {Function} commit commit
   * @param {boolean} isPageSwitching isPageSwitching
   */
  setPageSwitching({
    commit
  }, isPageSwitching) {
    commit(SET_PAGE_SWITCHING, isPageSwitching);
  },

  /**
   * 保存页面滚动位置
   *
   * @param {Function} commit commit
   * @param {Object} payload
   * @param {string} payload.pageId 页面 ID
   * @param {Object} payload.scrollPosition 滚动位置对象 {x:, y:}
   */
  savePageScrollPosition({
    commit
  }, {
    pageId,
    scrollPosition
  }) {
    commit(SET_PAGE_SCROLL_POSITION, {
      pageId,
      scrollPosition
    });
  },

  /**
   * 设置页面初始化的配置参数
   *
   * @param {Function} commit commit
   * @param {boolean} isPageSwitching isPageSwitching
   */
  setPageCommonParams({
    commit
  }, params) {
    commit(SAVE_PAGE_COMMON_PARAMS, params);
  },
  setVideo({
    commit
  }, params) {
    commit(SET_VIDEO_OBJ, params);
  }
};
